package com.gxuwz.business.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gxuwz.business.entity.Student;
import com.gxuwz.business.entity.Treat;
import com.gxuwz.util.Pager;

@Repository("treatDAO")
public class TreatDAO extends BaseDaoImpl<TreatDAO>{
	
	/**
	 * 添加
	 */
	public boolean add(Treat treat){
		boolean isSucceed = false;
		try {
			getHibernateTemplate().save(treat);
			isSucceed = true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return isSucceed;
	}

	/**
	 * 分页查询已开处方单的诊疗列表
	 */
	public Pager<Treat> getByPagerRec(Integer page, Integer limit){
		String hql = "from Treat where 1 = 1 and status = 0 and treatCount > 0";
		try {
			return find(hql, null, null, (page-1)*limit, limit);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * 分页查询(条件查询)
	 */
	public Pager<Treat> getByPager(Treat treat, Integer page, Integer limit){
		String hql = "from Treat where 1=1 and status = 0";
		//模糊查询
//		if(null != treat.getStuDesc() && !"".equals(treat.getStuDesc())){
//			String key = treat.getStuDesc();
//			hql += " and (stuId like '%"+key+"%' or stuName like '%"+key+"%' or stuSex like '%"+key+"%' "
//					+ "or stuAge like '%"+key+"%' or stuGrade like '%"+key+"%' or stuCollege like '%"+key+"%' "
//							+ "or stuMajor like '%"+key+"%' or careType like '%"+key+"%' or regType like '%"+key+"%')";
//		}
		//时间段查询
		if(null != treat.getOpenTime() && null != treat.getEndTime()){
			hql += "and regDate between '"+treat.getOpenTime()+"' and '"+treat.getEndTime()+"' ";
		}
		return super.find(hql, null, null, (page-1)*limit, limit);
	}
	
	public Treat findById(Treat treat){
		List list = super.findById(Treat.class, "treatId", treat.getTreatId());
		try {
			treat = (Treat) list.get(0);
			return treat;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 修改开处方次数以及诊疗状态
	 */
	public void updateCountState(Treat treat){
		String hql = "update Treat set treatCount = ?, state = ? where treatId = ?";
		try {
			getHibernateTemplate().bulkUpdate(hql, treat.getTreatCount(), treat.getState(), treat.getTreatId());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 更新
	 */
	public void update(Treat treat){
		try {
			getHibernateTemplate().update(treat);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取所有已开处方的诊编
	 */
	public List<String> getAllIdIsRecipe(){
		String hql = "select treatId from Treat where treatCount > 0";
		try {
			List<String> treatIdList = (List<String>) getHibernateTemplate().find(hql);
			return treatIdList;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}

}
